//index.js
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp();
Page({
  data: {
    userInfo: {},
    buttonLoading: false, 
    cate:'+',
    accountData:[],
    accountTotal:0,
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this;
    // 获取记录
    var tempAccountData = wx.getStorageSync("accountData") || [];
    this.caculateTotal(tempAccountData);
    this.setData({
        accountData: tempAccountData
    });
  },
  //   金额radio
  radioChange: function (e) {
    this.setData({
      cate:e.detail.value
    })
    console.log(e.detail.value)
  },
  // 计算总额
  caculateTotal:function(data){
      var tempTotal = 0;
      console.log(data);
      for(var x in data){
        console.log(data[x].amount);
          tempTotal += parseFloat(data[x].amount);
      }
      console.log("合计")
      console.log(tempTotal);
      this.setData({
        accountTotal: tempTotal
      });
  },
  //表单提交
  formSubmit:function(e){
      this.setData({
        buttonLoading: true
      });

      var that = this;
      setTimeout(function(){
          var inDetail = e.detail.value.inputdetail;
          var incate = that.data.cate;
          var inAmount1 = e.detail.value.inputamount;
          console.log(incate);
          console.log(e.detail.value.inputamount);
          if(incate == '+'){
            var inAmount = e.detail.value.inputamount;
          }else{
            var inAmount = -e.detail.value.inputamount;
          }
          console.log(inAmount);
         
          if(inDetail.toString().length <= 0 ){
            wx.showModal({
              title: '备注不能为空',
              showCancel:false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
              console.log("can not empty");
              that.setData({
                buttonLoading: false
              });
              return false;
          }
          else if (inAmount1.toString().length <= 0) {
            wx.showModal({
              title: '金额不能为空',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
            console.log("can not empty");
            that.setData({
              buttonLoading: false
            });
            return false;
          }
          
          //新增记录
          var tempAccountData = wx.getStorageSync("accountData") || [];
          tempAccountData.unshift({detail:inDetail,amount:inAmount});
          wx.setStorageSync("accountData",tempAccountData);
          console.log('accountData')
          that.caculateTotal(tempAccountData);
          that.setData({
              accountData: tempAccountData,
              buttonLoading: false
          });

      },1000);
  },
  //删除行
  deleteRow: function(e){
     var that = this;
     var index = e.target.dataset.indexKey;
     var tempAccountData = wx.getStorageSync("accountData") || [];
     tempAccountData.splice(index,1);
     wx.setStorageSync("accountData",tempAccountData);
     that.caculateTotal(tempAccountData);
     that.setData({
        accountData: tempAccountData,
     });
  }
})
